package com.example.day20_zhongji;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.ListFragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.TextView;

import com.example.day20_zhongji.adapter.VpFragmentAdapter;
import com.example.day20_zhongji.fragment.CradFragment;
import com.example.day20_zhongji.fragment.HomeFragment;
import com.example.day20_zhongji.fragment.ListlFragment;
import com.example.day20_zhongji.fragment.SqlFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private TextView mBarName;
    private Toolbar mToolbar;
    private ViewPager mViewpager;
    private TabLayout mTab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        mBarName = findViewById(R.id.bar_name);
        mToolbar = findViewById(R.id.toolbar);
        mViewpager = findViewById(R.id.viewpager);
        mTab = findViewById(R.id.tab);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ListlFragment());
        fragments.add(new CradFragment());
        fragments.add(new SqlFragment());
        VpFragmentAdapter vpFragmentAdapter = new VpFragmentAdapter(getSupportFragmentManager(), fragments);
        mViewpager.setAdapter(vpFragmentAdapter);
        mTab.setupWithViewPager(mViewpager);
        mTab.getTabAt(0).setText("首页").setIcon(R.drawable.home);
        mTab.getTabAt(1).setText("二级列表").setIcon(R.drawable.list);
        mTab.getTabAt(2).setText("卡片").setIcon(R.drawable.xing);
        mTab.getTabAt(3).setText("收藏").setIcon(R.drawable.mn);
        mTab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                switch (tab.getPosition()){
                    case 0:
                        mBarName.setText("首页");
                        break;
                    case 1:
                        mBarName.setText("二级列表");
                        break;
                    case 2:
                        mBarName.setText("卡片");
                        break;
                    case 3:
                        mBarName.setText("收藏");
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

}
